﻿package com.toncen.samepms.basic.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.toncen.samepms.common.AbstractDomain;
import com.toncen.samepms.system.dao.ConfigurationDao;
import com.toncen.samepms.system.domain.Role;
import com.sky.common.encrypt.Encryptor;
import com.sky.common.string.StringUtils;
import com.sky.simple.engine.IWorkflowParticipator;

/**
 * 描述：用户表BASIC_USER的domain文件
 * 
 * @author tch
 */
public class User extends AbstractDomain implements IWorkflowParticipator{
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1331547659424L;

	/**
	 * 组织
	 */
	private Organization organization;

	/**
	 * 姓名
	 */
	private String name;

	/**
	 * 用户帐号
	 */
	private String account;

	/**
	 * 用户密码
	 */
	private String password;

	/**
	 * 原始的密码(明文)
	 */
	private String originalPassword;

	/**
	 * 性别
	 */
	private int sex = 1;

	/**
	 * 出生日期
	 */
	private Date birthday = new Date();

	/**
	 * 身份证
	 */
	private String idcard;

	/**
	 * 工作电话
	 */
	private String workphone;

	/**
	 * 手机
	 */
	private String mobilephone;

	/**
	 * 邮箱地址
	 */
	private String email;

	/**
	 * 顺序
	 */
	private int sequence = 10;

	/**
	 * 
	 */
	private List<Role> roles = new ArrayList<Role>();

	/**
	 * @return the sequence
	 */
	public int getSequence() {
		return sequence;
	}

	/**
	 * @param sequence
	 *            the sequence to set
	 */
	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	public User() {
		super();
	}

	/**
	 * @return the organization
	 */
	public Organization getOrganization() {
		return organization;
	}

	/**
	 * @param organization
	 *            the organization to set
	 */
	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the account
	 */
	public String getAccount() {
		return account;
	}

	/**
	 * @param account
	 *            the account to set
	 */
	public void setAccount(String account) {
		this.account = account;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 * @throws Exception
	 */
	public void setPassword(String password) throws Exception {
		this.password = password;
		if (!StringUtils.isEmpty(password)) {// 解密
			this.originalPassword = Encryptor.decryptText((new ConfigurationDao().getByName("passkey").getValue()), password);
		}
	}

	/**
	 * @return the sex
	 */
	public int getSex() {
		return sex;
	}

	/**
	 * @param sex
	 *            the sex to set
	 */
	public void setSex(int sex) {
		this.sex = sex;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday
	 *            the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/**
	 * @return the idcard
	 */
	public String getIdcard() {
		return idcard;
	}

	/**
	 * @param idcard
	 *            the idcard to set
	 */
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	/**
	 * @return the workphone
	 */
	public String getWorkphone() {
		return workphone;
	}

	/**
	 * @param workphone
	 *            the workphone to set
	 */
	public void setWorkphone(String workphone) {
		this.workphone = workphone;
	}

	/**
	 * @return the mobilephone
	 */
	public String getMobilephone() {
		return mobilephone;
	}

	/**
	 * @param mobilephone
	 *            the mobilephone to set
	 */
	public void setMobilephone(String mobilephone) {
		this.mobilephone = mobilephone;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<Role> getRoles() {
		return roles;
	}

	/**
	 * @return the originalPassword
	 */
	public String getOriginalPassword() {
		return originalPassword;
	}

	/**
	 * @param originalPassword
	 *            the originalPassword to set
	 * @throws Exception
	 */
	public void setOriginalPassword(String originalPassword) throws Exception {
		this.originalPassword = originalPassword;
		if (!StringUtils.isEmpty(originalPassword)) {// 加密
			this.password = Encryptor.encryptText((new ConfigurationDao().getByName("passkey").getValue()), originalPassword);
		}
	}

	/**
	 * 获取缓存权限key:组织+用户+角色组合
	 * 
	 * @return
	 */
	public String getPermissionKey() {
		StringBuffer key = new StringBuffer();// 缓存权限key:组织+用户+角色组合
		if (this.getOrganization() != null) {// 组织
			key.append(Organization.class.getName());
			key.append("_");
			key.append(this.getOrganization().getId());
			key.append("_");
		}
		// 用户
		key.append(this.getClass().getName());
		key.append("_");
		key.append(this.getId());
		key.append("_");
		if (this.getRoles() != null) {// 角色
			for (Role role : this.getRoles()) {
				key.append(Role.class.getName());
				key.append("_");
				key.append(role.getId());
				key.append("_");
			}
		}
		return key.toString();
	}
}
